package cn.cstech.aijia.ui.base.title;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.EnumMap;

import cn.cstech.aijia.R;

public class NoTitlteBarContentView extends BaseContentView {

	private ViewGroup mContainerView;
	private ViewGroup mErrorViewPanel;
	private ViewGroup mErrorNetTopViewPanel;
	private ImageView mErrorIcon;
	private TextView mErrorText;
	private Button mErrorRetryButton;

	static EnumMap<ErrorType, ErrorInfo> mErrorDefaultMap;

	static {
		mErrorDefaultMap = new EnumMap<IActionTitleBar.ErrorType, ErrorInfo>(
				ErrorType.class);
		// 网络错误时头部的提示
		mErrorDefaultMap.put(ErrorType.ERRORTOP, new ErrorInfo(
				R.drawable.ic_send_fail, R.string.sm_error_type_top));
		// 列表为空时的显示
		mErrorDefaultMap.put(ErrorType.EMPTYVIEW, new ErrorInfo(
				R.drawable.error_empty_icon, R.string.sm_error_type_empty));
		// 网络加载数据错误
		mErrorDefaultMap.put(ErrorType.ERRORNET, new ErrorInfo(
				R.drawable.error_net_icon, R.string.sm_error_type_net));
		// 本地数据加载错误
		mErrorDefaultMap.put(ErrorType.ERRORRES, new ErrorInfo(
				R.drawable.error_src_icon, R.string.sm_error_type_res));

	}

	public NoTitlteBarContentView(Context context) {
		super(context);
	}

	public NoTitlteBarContentView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public NoTitlteBarContentView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public int onContentLayout() {
		Log.d("wyg", "onContentLayout---------->>");
		return R.layout.sm_no_titlebar_base_container_layout;
	}

	@Override
	public void onContentViewCreate(View view) {
		Log.d("wyg", "onContentViewCreate------->>");
		mContainerView = (ViewGroup) view.findViewById(R.id.sm_base_contaner);
		mErrorNetTopViewPanel = (ViewGroup) view
				.findViewById(R.id.sm_error_net_top_panel);
		mErrorViewPanel = (ViewGroup) view
				.findViewById(R.id.sm_error_display_panel);
		mErrorIcon = (ImageView) view.findViewById(R.id.sm_erro_display_icon);
		mErrorText = (TextView) view.findViewById(R.id.sm_erro_display_txt);
		mErrorRetryButton = (Button) view
				.findViewById(R.id.sm_erro_retry_button);
	}


	@Override
	public void setMainContentView(View view) {
		Log.d("wyg", "setMainContentView------->>");
		ViewGroup.LayoutParams vlp = new RelativeLayout.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT);
		mContainerView.addView(view, vlp);
	}

	@Override
	public void setTitleText(String title) {
	}

	@Override
	public void setTitleText(int title) {

	}

	@Override
	public void setLeftButton(String btnText, View.OnClickListener lsn) {

	}

	@Override
	public void setLeftButton(int btnText, View.OnClickListener lsn) {

	}

	@Override
	public void setLeftImageView(int imageRes, View.OnClickListener lsn) {

	}

	@Override
	public void setRightButton(String btnText, View.OnClickListener lsn) {

	}

	@Override
	public void hideRightButton() {

	}

	@Override
	public void setRightButton(int btnText, View.OnClickListener lsn) {

	}

	@Override
	public void setRightImageView(int imageRes, View.OnClickListener lsn) {

	}

	@Override
	public void showTitleBar(boolean show) {
	}

	@Override
	public void setDefaultBackActoin(View.OnClickListener lsn) {

	}

	@Override
	public void showErrorView(ErrorType type, String alertText,
			View.OnClickListener lsn) {
		showErrorView(type, -1, alertText, lsn);
	}

	@Override
	public void showErrorView(ErrorType type, int iconRes, String alertText,
			View.OnClickListener lsn) {
		if (type != null) {
			ErrorInfo eInfo = mErrorDefaultMap.get(type);
			if (iconRes <= 0) {
				mErrorIcon.setImageResource(eInfo.errorIconRes);
			} else {
				mErrorIcon.setImageResource(iconRes);
			}

			if (TextUtils.isEmpty(alertText)) {
				mErrorText.setText(eInfo.errorTextRes);
			} else {
				mErrorText.setText(alertText);
			}
			if (lsn != null) {
				mErrorRetryButton.setVisibility(VISIBLE);
				mErrorRetryButton.setOnClickListener(lsn);
			} else {
				mErrorRetryButton.setVisibility(GONE);
			}

			if (ErrorType.ERRORTOP == type) {
				mErrorNetTopViewPanel.setVisibility(VISIBLE);
			} else {
				mErrorViewPanel.setVisibility(VISIBLE);
			}
		}
	}

	@Override
	public void hideErrorView() {
		if (mErrorViewPanel != null) {
			mErrorViewPanel.setVisibility(GONE);
			mErrorNetTopViewPanel.setVisibility(GONE);
		}
	}

	@Override
	public void showErrorView(ErrorType type) {
		showErrorView(type, -1, null, null);
	}


	private static class ErrorInfo {
		public int errorIconRes;
		public int errorTextRes;

		public ErrorInfo() {
		}

		public ErrorInfo(int icon, int text) {
			errorIconRes = icon;
			errorTextRes = text;
		}
	}

}
